#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_geoopt_mp2.info <<'%EOF%'
   cc_geoopt_mp2
   -------------
   Molecule:         HF in C1
   Wave Function:    MP2 / aug-cc-pVDZ
   Test Purpose:     Geometry optimization using analytic gradient
                     from the CC program and the *OPTIMIZE module
%EOF%

#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_geoopt_mp2.mol <<'%EOF%'
INTGRL
first order property test calculation for hf                                    
aug-cc-pVDZ basis                                                               
    2  0 0  X  Y  Z   0.10D-14
        1.    1    2    1    1
H        0.0000000000000     0.0000000000000     1.6555844400000
H   4    2
       13.0100000000        0.0196850000        0.0000000000
        1.9620000000        0.1379770000        0.0000000000
        0.4446000000        0.4781480000        0.0000000000
        0.1220000000        0.0000000000        1.0000000000
H   1    1
        0.7270000000        1.0000000000
        9.    1    3    1    1    1
F        0.0000000000000     0.0000000000000    -0.1872947800000
H  17    3
    14710.0000000000        0.0007210000        0.0000000000        0.0000000000
     2207.0000000000        0.0055530000        0.0000000000        0.0000000000
      502.8000000000        0.0282670000        0.0000000000        0.0000000000
      142.6000000000        0.1064440000        0.0000000000        0.0000000000
       46.4700000000        0.2868140000        0.0000000000        0.0000000000
       16.7000000000        0.4486410000        0.0000000000        0.0000000000
        6.3560000000        0.2647610000        0.0000000000        0.0000000000
        1.3160000000        0.0153330000        0.0000000000        0.0000000000
    14710.0000000000        0.0000000000       -0.0001650000        0.0000000000
     2207.0000000000        0.0000000000       -0.0013080000        0.0000000000
      502.8000000000        0.0000000000       -0.0064950000        0.0000000000
      142.6000000000        0.0000000000       -0.0266910000        0.0000000000
       46.4700000000        0.0000000000       -0.0736900000        0.0000000000
       16.7000000000        0.0000000000       -0.1707760000        0.0000000000
        6.3560000000        0.0000000000       -0.1123270000        0.0000000000
        1.3160000000        0.0000000000        0.5628140000        0.0000000000
        0.3897000000        0.0000000000        0.0000000000        1.0000000000
H   4    2
       22.6700000000        0.0448780000        0.0000000000
        4.9770000000        0.2357180000        0.0000000000
        1.3470000000        0.5085210000        0.0000000000
        0.3471000000        0.0000000000        1.0000000000
H   1    1
        1.6400000000        1.0000000000
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_geoopt_mp2.dal <<'%EOF%'
**DALTON INPUT
.DIRECT
.OPTIMIZE
**INTEGRAL
.DIPLEN
.DEROVL
.DERHAM
**WAVE FUNCTION
.CC
*SCF INP
.THRESH
 1.0D-10
.DOUBLY OCCUPIED
 5
*CC INPUT
.MP2
**END OF DALTON INPUT
%EOF%
#######################################################################

 
#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_geoopt_mp2.check
cat >>cc_geoopt_mp2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# SCF, MP2 and CCSD energies of 0 iteration:
CRIT1=`$GREP -l "Final * HF energy: * -100\.01388196" $log | wc -l`
CRIT2=`$GREP -l "Total MP2   energy: * -100\.22012568" $log | wc -l`
CRIT3=`$GREP -l " H * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( |0)\.054453" $log | wc -l`
CRIT4=`$GREP -l " F * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( \-|\-0)\.054453" $log | wc -l`
CRIT5=`$GREP -l "Energy at this geometry is * : * -100\.220125" $log | wc -l`
CRIT6=`$GREP -l "Norm of gradient * : * ( |0)\.07700" $log | wc -l`
CRIT7=`$GREP -l "Norm of step * : * ( |0)\.08108" $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7` 
CTRL[1]=7
ERROR[1]="INITIAL ITERATION IS ALREADY NOT CORRECT"


# SCF, MP2 and CCSD energies of 1 iteration:
CRIT1=`$GREP -l "Final * HF energy: *-100\.01858250" $log | wc -l`
CRIT3=`$GREP -l "Total MP2   energy: *-100\.22301642" $log | wc -l`
CRIT4=`$GREP -l " H * ( |0|\-0|-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( |0)\.014716" $log | wc -l`
CRIT5=`$GREP -l " F * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( \-|\-0)\.014716" $log | wc -l`
CRIT6=`$GREP -l "Energy at this geometry is * : * -100\.22301" $log | wc -l`
CRIT7=`$GREP -l "Norm of gradient * : * ( |0)\.02081" $log | wc -l`
CRIT8=`$GREP -l " Norm of step * : * ( |0)\.03002" $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8` 
CTRL[2]=7
ERROR[2]="FIRST ITERATION IS ALREADY NOT CORRECT"


# SCF, MP2 and CCSD energies of 2 iteration:
CRIT1=`$GREP -l "Final * HF energy: *-100\.01943291" $log | wc -l`
CRIT3=`$GREP -l "Total MP2   energy: *-100\.22318964" $log | wc -l`
CRIT4=`$GREP -l " H * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( \-|\-0)\.003535" $log | wc -l`
CRIT5=`$GREP -l " F * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( |0)\.003535" $log | wc -l`
CRIT6=`$GREP -l "Energy at this geometry is * : * -100\.2231" $log | wc -l`
CRIT7=`$GREP -l "Norm of gradient * : * ( |0)\.00(499|500)" $log | wc -l`
CRIT8=`$GREP -l "Norm of step * : * ( |0)\.00581[5-8]" $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8` 
CTRL[3]=7
ERROR[3]="SECOND ITERATION IS NOT CORRECT"

# Final summary:
CRIT1=`$GREP -l " H * ( |0)\.0000000000 * ( |0)\.0000000000 * 1\.60280[23]" $log | wc -l`
CRIT2=`$GREP -l " F * ( |0)\.0000000000 * ( |0)\.0000000000 * ( \-|\-0)\.134513" $log | wc -l`
CRIT3=`$GREP -l "Geometry converged in * 5 *iterations!" $log | wc -l`
CRIT4=`$GREP -l "Energy at final geometry is * : *-100\.223199 a.u." $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2 + $CRIT3 \+ $CRIT4` 
CTRL[4]=4
ERROR[4]="FINAL SUMMARY IS NOT CORRECT"


PASSED=1
for i in 1 2 3 4
do 
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done 

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM 
  exit 1
fi
%EOF%
chmod +x cc_geoopt_mp2.check
#######################################################################
